package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.AddressManagementAdapter;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.AddressBean;
import com.dinadi.yidi.bean.AddressManagementEntity;
import com.dinadi.yidi.bean.AddressManagementRequest;
import com.dinadi.yidi.bean.MessageBean;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.ui.view.CommonDialogUtils;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/7/12  地址管理
 * Time: 18:28
 */
public class AddressManagementActivity extends BaseActivity {
    private View view;
    private TextView tvTitle;
    private ImageView new_address;
    private ImageView no_designer;
    private LinearLayout layout_blank_tips;
    private ListView lv_product_attribute;
    private CommonDialogUtils myDialog;
    private AddressManagementAdapter mRefreshAdapter;
    private AddressBean addressBean;
    private List<AddressManagementEntity> dataBean = new ArrayList<>();
    private List<AddressManagementEntity> dataBean2 = new ArrayList<>();
    private int page = 1;
    private String userId, token;
    private boolean isSelect;
    private int deletePosition, defaultPosition;

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(AddressManagementActivity.this).inflate(R.layout.activity_address_management, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {

        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText(R.string.text_address_management);
        new_address = (ImageView) view.findViewById(R.id.new_address);
        lv_product_attribute = (ListView) view.findViewById(R.id.lv_product_attribute);
        lv_product_attribute.setDivider(null);
        mRefreshAdapter = new AddressManagementAdapter(AddressManagementActivity.this, dataBean);
        lv_product_attribute.setAdapter(mRefreshAdapter);
        layout_blank_tips = (LinearLayout) view.findViewById(R.id.layout_blank_tips);
        no_designer = (ImageView) view.findViewById(R.id.no_designer);

    }

    @Override
    protected void initListener() {

        registerClickListener(new_address);
        mRefreshAdapter.setOnClickListenerCallBack(new AddressManagementAdapter.OnClickListenerCallBack() {
            @Override
            public void onClickDefault(int position, String id) {
                /**设置默认地址*/
                defaultPosition = position;
                AddressBean addressBean = new AddressBean();
                addressBean.setAddressId(id);
                addressBean.setToken(token);
                addressBean.setUserId(userId);
                CommonInterface deliveryAddressInterface = new CommonInterface(AddressManagementActivity.this, defaultHttpListener);
                deliveryAddressInterface.getInterface("16010", JSON.toJSONString(addressBean));

            }

            @Override
            public void onClickDelete(int position, String id) {
                /*删除地址*/
                deletePosition = position;
                addressBean = new AddressBean();
                addressBean.setAddressId(id);
                addressBean.setToken(token);
                myDialog = new CommonDialogUtils(AddressManagementActivity.this);
                myDialog.setContent(R.string.address_is_delect);
                myDialog.setDialogCallback(dialogcallback);
                myDialog.show();
            }

            @Override
            public void onClickInto(int position, String id) {
                /*跳转*/
                Intent intent = new Intent();
                intent.setClass(AddressManagementActivity.this, DeliveryAddressActivity.class);
                intent.putExtra("addressId", id);
                intent.putExtra("isModify", "1");
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        dataBean.clear();
        userId = SharePreferencesHelper.getInstance(AddressManagementActivity.this).getString("userId");
        token = SharePreferencesHelper.getInstance(AddressManagementActivity.this).getString("token");
        showLoadingDialog(getResources().getString(R.string.dialog_text));
        initPullrefresh();
    }

    @Override
    protected void initData() {
//        userId = SharePreferencesHelper.getInstance(AddressManagementActivity.this).getString("userId");
//        token = SharePreferencesHelper.getInstance(AddressManagementActivity.this).getString("token");
//        showLoadingDialog(getResources().getString(R.string.dialog_text));
//        initPullrefresh();

    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.new_address:
                Intent intent = new Intent(AddressManagementActivity.this, DeliveryAddressActivity.class);
                intent.putExtra("isSelect", isSelect);
                startActivity(intent);
                isSelect = false;
                break;
            default:
                break;
        }

    }

    private void initPullrefresh() {
        MessageBean bean = new MessageBean();
        bean.setToken(token);
        bean.setUserId(userId);
        bean.setPageNum(page + "");
        bean.setPageSize("100");
        CommonInterface messageInterface = new CommonInterface(AddressManagementActivity.this, httpListener);
        messageInterface.getInterface("16011", JSON.toJSONString(bean));
    }

    /**
     * 设置mydialog需要处理的事情
     */
    CommonDialogUtils.Dialogcallback dialogcallback = new CommonDialogUtils.Dialogcallback() {
        @Override
        public void dialogdo() {
            CommonInterface deliveryAddressInterface = new CommonInterface(AddressManagementActivity.this, httpListener2);
            deliveryAddressInterface.getInterface("16009", JSON.toJSONString(addressBean));
        }
    };
    /*修改默认*/
    HttpListener defaultHttpListener = new HttpListener() {
        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                for (int i = 0; i < dataBean.size(); i++) {
                    /**如果当前点击的position等于这个i的话，说明循环到了当前点击的内容*/
                    if (defaultPosition == i) {
                        /**然后改变它的状态*/
                        dataBean.get(i).setIsDefault("2");
                    } else {
                        /**把其他的状态全部清零*/
                        dataBean.get(i).setIsDefault("1");
                        /**这样应该就没问题了*/
                    }
                }
                //               initPullrefresh();
                mRefreshAdapter.notifyDataSetChanged();
            }

        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
        }
    };
    HttpListener httpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            dataBean.clear();
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                AddressManagementRequest bean = JsonUtil.parseJsonToBean(response, AddressManagementRequest.class);
                dataBean2 = bean.getDeliveryAddressList();
                if (dataBean2.size() == 0) {
                    layout_blank_tips.setVisibility(View.VISIBLE);
                        /*换图片*/
                    no_designer.setImageResource(R.drawable.llustration_address);

                    isSelect = true;
                } else {
                    layout_blank_tips.setVisibility(View.GONE);
                }

                dataBean.addAll(bean.getDeliveryAddressList());
                mRefreshAdapter.notifyDataSetChanged();

            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            closeLoadingDialog();
            toast("加载失败");
        }
    };
    /*删除地址*/
    HttpListener httpListener2 = new HttpListener() {
        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            myDialog.dismiss();
            if (errCode.equals("0000")) {
                ToastUtil.showToast("删除成功");
                initPullrefresh();
                if (dataBean.size() == 0) {
                    layout_blank_tips.setVisibility(View.VISIBLE);
                        /*换图片*/
                    no_designer.setImageResource(R.drawable.llustration_address);
                    isSelect = true;

                } else {
                    layout_blank_tips.setVisibility(View.GONE);
                }
                SharePreferencesHelper.getInstance(AddressManagementActivity.this).putString("isRefresh", "yes");

            }
        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
        }
    };


}
